﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.SXXK
{
    /// <summary>
    /// Nguyên phụ liệu nhập khẩu.
    /// </summary>
    public class NguyenPhuLieu
    {
        public static DataSet GetDanhSach(string maHaiQuan, string maDoanhNghiep)
        {
            string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
            string query = "SELECT NPL.Ma_HQ AS MaHaiQuan, NPL.Ma_NPL AS Ma, NPL.Ten_NPL AS Ten, NPL.Ma_HS AS MaHS, NPL.MA_DVT AS DVT_ID FROM SNPL NPL WHERE NPL.MA_HQ = @MA_HQ AND MA_DV = @MA_DV";

            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@Ma_DV", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maDoanhNghiep;

            return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);
        }

        public static bool CheckExist(SqlTransaction transaction, string maHaiQuan, string maDoanhNghiep, string maNPL)
        {
            string query = "SELECT COUNT(*) FROM SNPL WHERE (MA_HQ = @MaHaiQuan) AND (MA_DV = @MaDoanhNghiep) AND (MA_NPL = @MaNPL)";
            SqlParameter[] parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter("@MaHaiQuan", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@MaDoanhNghiep", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maDoanhNghiep;

            parameters[2] = new SqlParameter("@MaNPL", SqlDbType.VarChar);
            parameters[2].Size = 30;
            parameters[2].Direction = ParameterDirection.Input;
            parameters[2].Value = maNPL;

            object obj = SqlHelper.ExecuteScalar(transaction, CommandType.Text, query, parameters);
            if (obj != null)
            {
                if (Convert.ToInt32(obj) > 0)
                    return true;
            }
            return false;
        }

        public static string[] CheckExist(string maHaiQuan, string maDoanhNghiep, string[] danhSachMaNPL)
        {
            ArrayList temp = new ArrayList();
            string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    foreach (string maNPL in danhSachMaNPL)
                    {
                        if (CheckExist(trans, maHaiQuan, maDoanhNghiep, maNPL))
                        {
                            temp.Add(maNPL);
                        }
                    }
                    trans.Commit();
                }
                catch (Exception ex) 
                {
                    trans.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
            return (string[]) temp.ToArray(typeof(string));
        }
    }
}